package com.smartthinking.CICPNL.home.activity;

import java.util.ArrayList;
import java.util.Dictionary;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.res.Configuration;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.ViewGroup.LayoutParams;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;

import com.smartthinking.CICPNL.DatabaseHelper;
import com.smartthinking.CICPNL.R;
import com.smartthinking.CICPNL.home.objects.clsDictionary;

public class Plano extends Activity {
	private Dictionary dic;
	public Dialog progressDialog;
	private Intent intent;
	public int EVEIDR;
	public ArrayList<Dictionary<String, String>> TraeDatos = null;
	public static ArrayList<Dictionary<String,String>> Splash = null;

	
	public void voidFinish(View V) {
		finish();
	}
	public void onConfigurationChanged(Configuration newConfig) {
		super.onConfigurationChanged(newConfig);
		// Checks the orientation of the screen
	}
	public void abrirMenuLateral(View V) {
		intent = new Intent(Plano.this, MenuLateral.class);
		startActivity(intent);
	}
	
	@Override
	public void onBackPressed() {
	}	
	
	@SuppressLint("NewApi")
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.ui_plano);

		EVEIDR = Utilities.EVE_IDR;		
		dic = clsDictionary.getDctionary();
		CargarContenido();
	}

	private void CargarContenido() {
		// TODO Auto-generated method stub
		new AsyncActividades().execute("contenido");

	}

	private class AsyncActividades extends AsyncTask<String, Void, Integer> {
		int result = 0;
        String location = Environment.getExternalStorageDirectory().toString()+"/Pediatria/";


		protected void onPreExecute() {
			progressDialog = ProgressDialog.show(Plano.this, "",getResources().getString(R.string.msg_cargando));
		}

		@SuppressLint("NewApi")
		protected Integer doInBackground(String... urls) {
			DatabaseHelper usdbh = new DatabaseHelper(getApplicationContext());
	        SQLiteDatabase db = usdbh.getWritableDatabase();
	        String qry_local = "select eve_plano from eventos where eve_idr = 1";
	        //Si hemos abierto correctamente la base de datos
	        if(db != null)
	        {
	        	Cursor cursor = db.rawQuery(qry_local, null);
	        	if(cursor.moveToFirst()){
	        		result = 1;
	        		location += cursor.getString(0);
	        	}else{
	        		result = 0;
	        	}
	        	db.close();
	        }
	        qry_local = "select * from expositores";
	        TraeDatos = Utilities.qry2(getApplicationContext(), qry_local);
	        
	        String qry = "select a.* from eventos_banners a inner join banners_catalogo b on a.ban_idr = b.ban_idr where a.eve_idr = "+EVEIDR+" and b.ban_nombre = 'SPLASH PLANO' ORDER BY b.ban_nombre";
			Splash = Utilities.qry2(getApplicationContext(), qry);	
			
			return null;
		}
		
		
		
		protected void onPostExecute(Integer in) {
			if(result == 0){
				
			}else{
				WebView  webView = (WebView)findViewById(R.id.webView1);
				RelativeLayout relative = (RelativeLayout) findViewById(R.id.relative_xy);
				
			    webView.getSettings().setBuiltInZoomControls(true);
			    webView.getSettings().setJavaScriptEnabled(true);
			   

			    String url ="file://"+location;
			    String imgSrcHtml = "<html><img src='" + url + "' /></html>";
			    webView.loadDataWithBaseURL("", imgSrcHtml, "text/html","utf-8", "");
			    
			    //presento splash
		    	if(Splash.size()>0){
			    	dic = Splash.get(0);
			    	String strPoster = dic.get("ban_url").toString();
				    String strTimer = dic.get("ban_timer").toString();
				    String strPatrocinador = dic.get("pat_idr").toString();
				    if (strPoster.equals("null")) {
				        strPoster = "";
				    }
				    if(strPoster.length()>0){
						intent = new Intent(Plano.this, Splash2.class);
						Bundle bund = new Bundle();
						bund.putString("PUT_IMG",strPoster);
						bund.putString("PUT_PAT",strPatrocinador);
						bund.putString("PUT_TIME",strTimer);
						intent.putExtras(bund);
						startActivity(intent);
				    }
			    }
			    
			    
//			    Button bt = new Button(getApplicationContext());
//	    		RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
//	    		lp.addRule(RelativeLayout.ALIGN_PARENT_TOP);
//	    		lp.setMargins(100, 100, 0, 0);
//	    		bt.setLayoutParams(lp);
//	    		bt.setBackgroundResource(R.drawable.checked);
//	    		relative.addView(bt);
			    
//			    for(int i = 0; i < TraeDatos.size(); i++){
//			    	Dictionary<String , String> dic = TraeDatos.get(i);
//			    	if((dic.get("exp_croquis_x").equals("")) || (dic.get("exp_croquis_y").equals("")) || 
//			    			(dic.get("exp_croquis_y").equals("null")) || (dic.get("exp_croquis_y").equals("null"))){
//			    	}else{
//			    		Button bt = new Button(getApplicationContext());
//			    		RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
//			    		lp.addRule(RelativeLayout.ALIGN_PARENT_TOP);
//			    		lp.setMargins(Integer.parseInt(dic.get("exp_croquis_x")), Integer.parseInt(dic.get("exp_croquis_y")), 0, 0);
//			    		bt.setLayoutParams(lp);
//			    		bt.setBackgroundResource(R.drawable.checked);
//			    		relative.addView(bt);
//			    	}
//			    	
//			    }
			    
			    
			}
			
			progressDialog.dismiss();
		}

	} 
}
